﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsxyUIS.Framework
{
    public class EasyuiHtmlHelper
    {
        public static IDictionary<string, object> Create(EasyuiType easyuiType, object options)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("class", GetEeasyuiClassName(easyuiType));
            if (options == null)
            {
                return dictionary;
            }

            foreach (var p in options.GetType().GetProperties())
            {
                var name = p.Name.Equals("options", StringComparison.CurrentCultureIgnoreCase) ? "data-options" : p.Name;
                var items = dictionary.Where(d => d.Key == name).ToList();
                if (items.Count() > 0 && EasyuiMergeAttributes.Contains(p.Name))
                {
                    var item = items[0];
                    var val = p.GetValue(options);
                    dictionary.Add(name, item.Value + (val == null ? "" : ((string.IsNullOrWhiteSpace(item.Value.ToString()) ? "" : " ") + val.ToString())));
                    dictionary.Remove(item);
                }
                else 
                {

                    dictionary.Add(name, p.GetValue(options));
                }
            }
            return dictionary;
        }

        public static IDictionary<string, object> Create(object options)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object>();
            if (options == null)
            {
                return dictionary;
            }
            foreach (var p in options.GetType().GetProperties())
            {
                var name = p.Name.Equals("options", StringComparison.CurrentCultureIgnoreCase) ? "data-options" : p.Name;
                dictionary.Add(name, p.GetValue(options));
            }
            return dictionary;
        }


        public static string[] EasyuiMergeAttributes = new string[] { "class", "style" };


        public static string GetEeasyuiClassName(EasyuiType input)
        {
            return Enum.GetName(input.GetType(), input);
        }
    }

    public enum EasyuiType
    {
        Draggable,

        Droppable,

        Resizable,

        Pagination,

        Searchbox,

        Progressbar,

        Tooltip,

        Panel,

        Tabs,

        Accordion,

        Layout,

        Menu,

        LinkButton,

        MenuButton,

        SplitButton,

        Form,

        ValidateBox,

        Combo,

        ComboBox,

        ComboTree,

        ComboGrid,

        NumberBox,

        DateBox,

        DateTimeBox,

        Calendar,

        Spinner,

        NumberSpinner,

        TimeSpinner,

        Slider,

        Window,

        Dialog,

        Messager,

        DataGrid,

        PropertyGrid,

        Tree,

        TreeGrid
    }
}
